// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.

import Vue from 'vue'
import App from './App'
import BootstrapVue from 'bootstrap-vue'
import vSelect from 'vue-select'
import router from './router'
import store from './vuex/mobile'
import axios from 'axios'
import qs from 'qs'
import VeeValidate, {Validator} from 'vee-validate'
import zhCN from 'vee-validate/dist/locale/zh_CN'
import Message from 'vue-m-message'
import {getCookie} from './utils/utils'
import constant from './utils/constant'

import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'
import 'vue-select/dist/vue-select.css'
import './assets/app.css'

Vue.use(BootstrapVue)
Vue.use(Message)

Vue.component('v-select', vSelect)
Vue.prototype.axios = axios
Vue.prototype.qs = qs

// 自定义validate
const config = {
  aria: true,
  classNames: {},
  classes: false,
  delay: 0,
  dictionary: {
    zhCN
  },
  errorBagName: 'errors', // change if property conflicts
  events: 'input|blur',
  fieldsBagName: 'fields',
  i18n: null, // the vue-i18n plugin instance
  i18nRootKey: 'validations', // the nested key under which the validation messages will be located
  inject: true,
  locale: 'zhCN',
  validity: false,
  useConstraintAttrs: true
}

Vue.use(VeeValidate, config)

Validator.extend('airportNumber', {
  getMessage: '无效的航班号',
  validate: value => /^[a-zA-Z0-9]{4,}$/.test(value)
})

Validator.extend('phone', {
  getMessage: function() {
    return '无效的手机号'
  },
  validate: value => {
    return (value.length === 11 && /^((13|14|15|17|18|19)[0-9]{1}\d{8})$/.test(value)) || /^[+]852\d{8}$/.test(value) || /^[+]853\d{8}$/.test(value)
  }
})

const dictionary = {
  zhCN: {
    messages: {
      required: field => '请输入' + field,
      alpha: field => '请使用' + field + '拼音',
      email: field => '请填写正确的邮箱'
    }
  }
}

Validator.localize(dictionary)

Vue.config.productionTip = false

// 处理刷新的时候vuex被清空但是用户已经登录的情况
const mobileCookie = getCookie(constant.mobileCookie)
if (mobileCookie) {
  if (/^\+/.test(mobileCookie)) {
    store.dispatch('setMobile', mobileCookie)
  } else {
    store.dispatch('setMobile', JSON.parse(mobileCookie))
  }
}

// 登录中间验证，页面需要登录而没有登录的情况直接跳转登录
router.beforeEach((to, from, next) => {
  let meta1 = document.createElement('meta')
  let meta2 = document.createElement('meta')
  meta1.name = 'keywords'
  meta2.name = 'description'
  meta1.setAttribute('content', 'alamo、爱路美、荷美邮轮、世邦邮轮、斐济航空、迪士尼邮轮、达拉斯机场、波特兰旅游局、俄勒冈旅游局')
  meta2.setAttribute('content', '飞腾会（FitaClub），是一个旅游资源共享品牌，致力于打造赋能型旅游新平台为旅游同业提供产品服务、提供智能营销，旅游资源撮合等服务，助力旅行社及旅游社群的转化升级目前该平台聚集了专业的旅游代理商，业务涉及国际机票、国际租车、奢华邮轮、目的地营销等产品及相关服务平台会定期发布最新旅游资讯，组织旅游峰会以及线上培训等')

  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (store.state.mobile) {
      next()
    } else {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    }
  } else {
    next()
  }

  document.title = to.meta.title || '飞腾会-Fita Club'
  document.head.appendChild(meta1)
  document.head.appendChild(meta2)
  next()
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>',
  store
})
